import axios from "axios";
import md5 from "md5";
import querystring from "querystring";
import { getOptions } from "./options";

const { appId: id, key: k } = getOptions();
const url = "https://fanyi-api.baidu.com";
const appid = id || "20220809001299926";
const key = k || "Je266zb0Ali7SqAeSjCS";
axios.defaults.baseURL = url;
interface Parma {
  q: string;
  from: string;
  to: string;
  appid: string;
  salt: string;
  sign: string;
}
export const transform = (q: string, target: string, from = "auto") => {
  const salt = Math.random() + "";
  const parma: Parma = {
    q,
    from,
    appid,
    to: target,
    salt,
    sign: md5(appid + q + salt + key),
  };
  return axios.get(
    `/api/trans/vip/translate?${querystring.stringify(
      parma as unknown as querystring.ParsedUrlQueryInput
    )}`
  );
};
